package ru.ifmo.thesis.solver.gp;

import org.jgap.Genotype;

import ru.ifmo.thesis.quantities.Minutes;

public class FixedTimeCalculationRule implements CompletionRule {

	private static final int SEC_IN_MIN = 60;

	private static final int NANOS_IN_SEC = (int) Math.pow(10, 9);

	private long started = System.nanoTime();
	
	private long duration;
	
	private Minutes durationMinutes;
	
	public FixedTimeCalculationRule(Minutes minutes) {
		this.durationMinutes = minutes;
		long lMinutes = minutes.value().longValue();
		duration = lMinutes * SEC_IN_MIN * NANOS_IN_SEC;
	}
	
	@Override
	public boolean isTaskCompleted(Genotype population) {
		return (System.nanoTime() - started) >= duration;
	}

	@Override
	public CompletionRule newCopy() {
		return new FixedTimeCalculationRule(durationMinutes);
	}

}
